package com.example.ccj.u.data;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.LogUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * SharedPreferences永久存储数据，用来其他界面调用
 * 2023年11月5日18:26:15
 */
public class YongJiuCunChu {
    private static final String TAG = "永久存储YongJiuCunChu";
    private static YongJiuCunChu instance;

    public static YongJiuCunChu getInstance() {
        if (instance == null) {
            synchronized (YongJiuCunChu.class) {
                if (instance == null) {
                    instance = new YongJiuCunChu();
                }
            }
        }
        return instance;
    }

    //永久存储
    private SharedPreferences getShared(){
        Activity activity = ActivityUtils.getTopActivity();
        return activity.getSharedPreferences("data",Context.MODE_PRIVATE);
    }

    //永久存储
    private SharedPreferences.Editor getEditor(){
        Activity activity = ActivityUtils.getTopActivity();
        SharedPreferences sharedPreferences;
        if (activity != null) {
            sharedPreferences = activity.getSharedPreferences("data",Context.MODE_PRIVATE);
            return sharedPreferences.edit();
        }
        LogUtils.d(TAG, "getEditor() 永久数据存储: " + "未获取到activity，创建本地数据失败!");
        return null;
    }

    public String getString(String key){
        SharedPreferences preferences = getShared();
        return preferences.getString(key,null);
    }
    public int getInt(String key){
        SharedPreferences preferences = getShared();
        return preferences.getInt(key,0);
    }

    public long getLong(String key){
        SharedPreferences preferences = getShared();
        return preferences.getLong(key,0);
    }

    public boolean getBoolean(String key){
        SharedPreferences preferences = getShared();
        return preferences.getBoolean(key,false);
    }

    public Set<String> getSet(String key){
        SharedPreferences preferences = getShared();
        Set<String> strings = new HashSet<>();
        return preferences.getStringSet(key,strings);
    }

    public boolean setData(String key,String value){
        SharedPreferences.Editor edit = getEditor();
        if (edit != null) {
            edit.putString(key, value);
            edit.apply();
            return true;
        }
        return false;
    }

    public boolean removeAll(){
        SharedPreferences.Editor edit = getEditor();
        if (edit != null) {
            edit.clear();
            edit.apply();
            return true;
        }
        return false;
    }

    public boolean removeOne(String key){
        SharedPreferences.Editor edit = getEditor();
        if (edit != null) {
            edit.remove(key);
            edit.commit();
            return true;
        }
        return false;
    }


    public boolean setData(String key,int value){
        SharedPreferences.Editor edit = getEditor();
        if (edit != null) {
            edit.putInt(key, value);
            edit.apply();
            return true;
        }
        return false;
    }

    public boolean setData(String key,long value){
        SharedPreferences.Editor edit = getEditor();
        if (edit != null) {
            edit.putLong(key, value);
            edit.apply();
            return true;
        }
        return false;
    }

    public boolean setData(String key,boolean value){
        SharedPreferences.Editor edit = getEditor();
        if (edit != null) {
            edit.putBoolean(key, value);
            edit.apply();
            return true;
        }
        return false;
    }

    public boolean setData(String key, Set<String> value){
        SharedPreferences.Editor edit = getEditor();
        if (edit != null) {
            edit.putStringSet(key, value);
            edit.apply();
            return true;
        }
        return false;
    }


}
